import Vue from 'vue'
import VueRouter from 'vue-router'
import Manager from '../views/Manager.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home',
    component: Manager,
    children:[
      //通用页面
      {path:'home',meta:{name: '首页'},component: ()=>import('../views/main/Home.vue')},
      {path:'user',meta:{name: '用户管理'},component: ()=>import('../views/main/user/user.vue')},
      {path:'person',meta:{name: '个人信息'},component: ()=>import('../views/main/Person.vue')},
      {path:'changePassword',meta:{name: '修改密码'},component: ()=>import('../views/main/PasswordChange.vue')},
      //消息页面
      {path:'message', meta:{name: '消息列表'},component: ()=>import('../views/main/message/Message.vue')},
      //设备模块
      {path:'device',meta:{name: '消防设备'},component: ()=>import('../views/main/device/device.vue')},
      {path:'ownerDevice',meta:{name: '消防设备'},component: ()=>import('../views/main/device/owner-device.vue')},
      //建筑模块
      {path:'building',meta:{name: '楼栋管理'},component: ()=>import('../views/main/house/building.vue')},
      {path:'floor',meta:{name: '楼层管理'},component: ()=>import('../views/main/house/floor.vue')},
      {path:'house',meta:{name: '房屋管理'},component: ()=>import('../views/main/house/house.vue')},
      {path:'houseBasicInfo',meta:{name: '房屋基本信息'},component: ()=>import('../views/main/house/house-basic-info.vue')},
      {path:'houseSafetyStatus',meta:{name: '房屋安全状况'},component: ()=>import('../views/main/house/house-safety-status.vue')},
      {path:'analysis',meta:{name: '统计分析'},component: ()=>import('../views/main/inspection/analysis.vue')},
      {path:'responsibleFor',meta:{name: '负责对象'},component: ()=>import('../views/main/house/responsible-for.vue')},
      //安全检查模块
      {path:'inspection',meta:{name: '安全检查记录'},component: ()=>import('../views/main/inspection/inspection.vue')},
      {path:'inspector',meta:{name: '安全检查员'},component: ()=>import('../views/main/inspection/inspector.vue')},
      {path:'record',meta:{name: '检查记录'},component: ()=>import('../views/main/inspection/record.vue')},
      //业主模块
      {path:'owner',meta:{name: '业主管理'},component: ()=>import('../views/main/owner/owner.vue')},
      //公告模块
      {path:'noticeManage',meta:{name: '公告管理'},component: ()=>import('../views/main/notice/notice-manage.vue')},
      {path:'notice',meta:{name: '公告'},component: ()=>import('../views/main/notice/notice.vue')},
      // {path:'repairman',meta:{name: '修理工管理'},component: ()=>import('../views/main/repair/repairman.vue')},
      // {path:'repairrecord',meta:{name: '修理记录'},component: ()=>import('../views/main/repair/repairrecord.vue')},
    ]
  },
  {
    path: '/403',
    name: '403View',
    component: ()=>import('../views/403.vue'),
    meta:{name: '无权限'},
  },
  {
    path: '*',
    name: '404View',
    component: ()=>import('../views/404.vue'),
    meta:{name: '找不到页面'},
  },
  {
    path: '/login',
    name: 'LoginView',
    component: ()=>import('../views/LoginView.vue'),
    meta:{name: '登录'},
  },
  {
    path: '/register',
    name: 'RegisterView',
    component: ()=>import('../views/Register.vue'),
    meta:{name: '注册'},
  },
]

const router = new VueRouter({
  mode: 'history',
  routes
})

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 路由守卫
//to:去哪里的信息，from是从哪里的信息，next是必须要执行的下一步
router.beforeEach((to,from,next)=>{
  let adminPaths=['/user']
  let loginUser=JSON.parse(localStorage.getItem("user")) || {}
  //不能让普通用户到管理用户页面
  if(loginUser.role==='用户'&&adminPaths.includes(to.path))
    next('/403')
  else
    next()
})

export default router
